package com.example.demo.config;

import com.example.demo.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

import java.io.IOException;


/**
 * @description：WebMvc配置.
 * @author：lw
 * @since：2023/12/29
 **/
@Component
public class WebMvcConfig implements WebMvcConfigurer{

    @Autowired
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry
                .addMapping("/**")      // 添加路径规则
                .allowCredentials(true)           // 是否允许携带凭证（如cookies）
                .allowedOriginPatterns("*")       // 允许请求来源的域规则
                .allowedMethods("*")             // 允许的方法，get、post等
                .allowedHeaders("*")             // 允许所有的请求头
                .maxAge(3600);                  // 预检请求的有效期（秒）
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry
                .addInterceptor(tokenInterceptor)
                .excludePathPatterns(ConstantConfig.noLoginUrls)
                .addPathPatterns("/**");

    }
}

